package com.lz.fruitday.fragment;


import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.lz.fruitday.R;
import com.lz.fruitday.adapter.BaikeHeadAdapter;
import com.lz.fruitday.adapter.FoodLvAdapter;
import com.lz.fruitday.bean.BaikeHeadBean;
import com.lz.fruitday.bean.FoodBaseBean;
import com.lz.fruitday.bean.FoodMainBean;
import com.lz.fruitday.constant.Path;
import com.lz.fruitday.widget.GridViewForScrollView;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;

import cz.msebera.android.httpclient.Header;

/**
 * A simple {@link Fragment} subclass.
 */
public class BaiKeFragment extends Fragment {

    private int page=1;

    @ViewInject(R.id.ptr_food_baike)
    private PullToRefreshListView ptrFoodBaike;
    private ListView lvFood;
    private AsyncHttpClient client=new AsyncHttpClient();
    private ArrayList<FoodMainBean> mainBeans=new ArrayList<>();
    private FoodLvAdapter lvAdapter;
    //头部Geidview相关
    private View headView;
    private GridViewForScrollView gvBaike;
    private ArrayList<BaikeHeadBean> headBeans=new ArrayList<>();
    private BaikeHeadAdapter headAdapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       View view= inflater.inflate(R.layout.fragment_bai_ke, container, false);
        ViewUtils.inject(this, view);
        addHead(inflater);
        initView();
        initData();
       ptrFoodBaike.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
           @Override
           public void onRefresh(PullToRefreshBase<ListView> refreshView) {
               page++;
               initData();
           }
       });
        return view;
    }

    private void initData() {
        client.post(Path.PATH_FOOD, getFParams(), new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                TypeReference<FoodBaseBean<FoodMainBean>> typeReference = new TypeReference<FoodBaseBean<FoodMainBean>>() {
                };
                FoodBaseBean baseBean = JSONObject.parseObject(responseString, typeReference);
                mainBeans.addAll(baseBean.getMain());
            }
        });
    }

    private RequestParams getFParams() {
        RequestParams params=new RequestParams();
        params.add("sign","08260bad95abac58a1077e3101166db1");
        params.add("limit","20");
        params.add("timestamp","1445429278");
        params.add("platform","ANDROID");
        params.add("source","app");
        params.add("page",String.valueOf(page));
        params.add("connect_id","161c2ae532723d329ee711d284a1b36e");
        params.add("region_id","106092");
        params.add("service","bake.getArticleList");
        params.add("device_id","1b8ba62bf241cee5b8c317d07c0c503e");
        params.add("channel","portal");
        params.add("version","3.0.3");
        return params;
    }

    private void initView() {
        ptrFoodBaike.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        lvFood=ptrFoodBaike.getRefreshableView();
        lvFood.addHeaderView(headView);
        lvAdapter = new FoodLvAdapter(mainBeans,getActivity());
        lvFood.setAdapter(lvAdapter);
    }
    private void addHead(LayoutInflater inflater) {
        headView = inflater.inflate(R.layout.item_add_baike_layout,null);
        gvBaike = (GridViewForScrollView) headView.findViewById(R.id.food_baike_gv);
        headAdapter = new BaikeHeadAdapter(headBeans,getActivity());
        gvBaike.setAdapter(headAdapter);
        client.post(Path.PATH_FOOD_HEAD, getHParams(), new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }
            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
                    JSONArray array=new JSONArray(responseString);
                    for (int i = 0; i < array.length(); i++) {
                        BaikeHeadBean bean=new BaikeHeadBean();
                        org.json.JSONObject o=array.getJSONObject(i);
                        bean.setName(o.getString("name"));
                        bean.setPhoto(o.getString("photo"));
                        bean.setNum(o.getString("num"));
                        headBeans.add(bean);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                headAdapter.notifyDataSetChanged();
            }
        });
    }

    private RequestParams getHParams() {
        RequestParams params=new RequestParams();
        params.add("sign","2a60b2768ce6dced38b414197ac79929");
        params.add("timestamp","1445429278");
        params.add("platform","ANDROID");
        params.add("source","app");
        params.add("connect_id","161c2ae532723d329ee711d284a1b36e");
        params.add("region_id","106092");
        params.add("service", "bake.getSectionList");
        params.add("device_id", "1b8ba62bf241cee5b8c317d07c0c503e");
        params.add("channel", "portal");
        params.add("version", "3.0.3");
        return params;
    }


}
